package com.nicky.kids.Test;


import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.os.Parcelable;
import android.provider.MediaStore;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;

import com.nicky.kids.ModifyActivity;
import com.nicky.kids.R;
import com.nicky.kids.Utility.DateUtil;
import com.nicky.kids.Utility.IntentUtil;
import com.nicky.kids.Utility.SDCardUtil;
import com.nicky.kids.Utility.TLog;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class TestMainActivity extends Activity {

    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ActionBarDrawerToggle mDrawerToggle;
    private ViewPager mViewPager;

//    ListView listview;
    List<String> listItems;

    final String MENU_HOME = "home";
    final String MENU_FRIEND= "friend";
    final String MENU_EXPLORE = "explore";
    final String MENU_NEW = "new";

    StringBuffer mPhotoName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        listItems = new ArrayList<String>();
        for (int i=0; i<5; i++) {
            listItems.add("hehe "+i);
        }

        //listview = (ListView)this.findViewById(R.id.list);
        //listview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, listItems));

        //setListViewHeightBasedOnChildren(listview);

        Button btn = new Button(this);
        btn.setText("test");
        getActionBar().setCustomView(btn);
        //Drawable drawable = this.getResources().getDrawable(R.drawable.button_p);
        //this.getSupportActionBar().setBackgroundDrawable(drawable);


        //drawer
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        // set a custom shadow that overlays the main content when the drawer opens
        //mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
        // set up the drawer's list view with items and click listener
        mDrawerList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, listItems));
        //mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);

        // ActionBarDrawerToggle ties together the the proper interactions
        // between the sliding drawer and the action bar app icon
        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                R.drawable.ic_drawer,  /* nav drawer image to replace 'Up' caret */
                R.string.hello_world,  /* "open drawer" description for accessibility */
                R.string.hello_world   /* "close drawer" description for accessibility */
        ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle("Home");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle("Settings");
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);



        //viewpager

        mViewPager = (ViewPager) findViewById(R.id.pager);
        ArrayList<View> listViews = new ArrayList<View>();
        LayoutInflater mInflater = getLayoutInflater();
        listViews.add(mInflater.inflate(R.layout.activity_home, null));
        listViews.add(mInflater.inflate(R.layout.activity_friend, null));
        listViews.add(mInflater.inflate(R.layout.activity_find, null));
        listViews.add(mInflater.inflate(R.layout.activity_detail, null));

        mViewPager.setAdapter(new MyPagerAdapter(listViews));
        mViewPager.setCurrentItem(0);
        //mViewPager.setOnPageChangeListener(new MyOnPageChangeListener());
    }

    public  void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();

        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        //listView.getDividerHeight()获取子项间分隔符占用的高度

        listView.setLayoutParams(params);
    }

    class TestPagerListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onPageSelected(int arg0) {
            getActionBar().selectTab(getActionBar().getTabAt(arg0));
        }
    }

    public class MyPagerAdapter extends PagerAdapter {
        public List<View> mListViews;

        public MyPagerAdapter(List<View> mListViews) {
            this.mListViews = mListViews;
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            ((ViewPager) arg0).removeView(mListViews.get(arg1));
        }

        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public int getCount() {
            return mListViews.size();
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            ((ViewPager) arg0).addView(mListViews.get(arg1), 0);
            return mListViews.get(arg1);
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.home, menu);

//        MenuItemCompat.setShowAsAction(
//            menu.add(MENU_FRIEND).setIcon(android.R.drawable.ic_menu_gallery),
//            MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
//        MenuItemCompat.setShowAsAction(
//            menu.add(MENU_EXPLORE).setIcon(android.R.drawable.ic_menu_compass),
//            MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
//        MenuItemCompat.setShowAsAction(
//            menu.add(MENU_NEW).setIcon(android.R.drawable.ic_menu_camera),
//            MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);

        menu.add(MENU_FRIEND).setIcon(android.R.drawable.ic_menu_gallery).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        menu.add(MENU_EXPLORE).setIcon(android.R.drawable.ic_menu_compass).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        menu.add(MENU_NEW).setIcon(android.R.drawable.ic_menu_camera).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

        return true;
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        // If the nav drawer is open, hide action items related to the content view
//        boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
//        menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (mDrawerToggle.onOptionsItemSelected(item)) {
            return true;
        }

        switch (item.getItemId()) {
//            case android.R.id.home:
//                // 当Action Bar的图标被单击时执行下面的Intent
//                Intent intent = new Intent(this, Android123.class);
//                startActivity(intent);
//                break;
        }

        TLog.e("nick", "menu selected: "+item.getTitle());

        String selectedItem = item.getTitle().toString();
        if (selectedItem.equals(MENU_FRIEND)) {

        } else if (selectedItem.equals(MENU_EXPLORE)) {

        } else if (selectedItem.equals(MENU_NEW)) {
            try {
                mPhotoName = new StringBuffer();
                mPhotoName.append("IMG_");
                mPhotoName.append(DateUtil.dateToString());
                mPhotoName.append(".jpg");

                //TLog.e(TAG, mPhotoName.toString());

                File mPhotoFile = new File(SDCardUtil.getKidsDir(), mPhotoName.toString());
                if (!mPhotoFile.exists()) {
                    mPhotoFile.createNewFile();
                }

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mPhotoFile));
                startActivityForResult(intent, 0);




                //choose pic
//                    Intent intent = new Intent();
//                    intent.setType("image/*");
//                    intent.setAction(Intent.ACTION_GET_CONTENT);
//                    startActivityForResult(intent, 2);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }


        return super.onOptionsItemSelected(item);
    }

    /**
     * When using the ActionBarDrawerToggle, you must call it during
     * onPostCreate() and onConfigurationChanged()...
     */

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        // Sync the toggle state after onRestoreInstanceState has occurred.
        mDrawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        // Pass any configuration change to the drawer toggls
        mDrawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0) {
            Intent intent = new Intent(TestMainActivity.this, ModifyActivity.class);
            intent.putExtra("name", mPhotoName.toString());
            IntentUtil.startIntent(TestMainActivity.this, intent);
        }

    }
}
